package com.xxg.renrenuser.controller;

import com.alibaba.fastjson.JSON;
import com.xxg.renrencommon.domain.result.BaseResult;
import com.xxg.renrencommon.exception.exceptionenum.UserExceptionCodeCodeEnum;
import com.xxg.renrenuser.domain.query.ExpressQuery;
import com.xxg.renrenuser.manager.KdniaoTrackQueryAPI;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 快递跟踪接口
 *
 * @author xxg
 * @date 2020/12/19
 */
@Api(tags = "快递跟踪接口")
@RestController
@RequestMapping("/expressTrack")
public class ExpressTrackController {
    private static final Logger logger = LoggerFactory.getLogger(ExpressTrackController.class);
    private final KdniaoTrackQueryAPI kdniaoTrackQueryAPI;

    public ExpressTrackController() {
        kdniaoTrackQueryAPI = new KdniaoTrackQueryAPI();
    }

    @ApiOperation("查询快递信息")
    @RequestMapping(value = "queryTrace", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "expressQuery", value = "代码表信息", required = true, dataType = "ExpressQuery")})
    public BaseResult queryTrace(@RequestBody @Validated ExpressQuery expressQuery) {
        String result;
        try {
            result = kdniaoTrackQueryAPI.getOrderTracesByJson(expressQuery.getExpCode(), expressQuery.getExpNo(), expressQuery.getCustomerPhone());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new BaseResult(UserExceptionCodeCodeEnum.EXPRESS_QUERY_ERROR, e.getMessage(), null);
        }
        return new BaseResult(JSON.parse(result));
    }
}
